package com.example.zyfypt516wh.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.*;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.bean.UserBean;
import com.example.zyfypt516wh.iface.LoginListener;
import com.example.zyfypt516wh.model.LoginModel;

public class LoginActivity extends AppCompatActivity {

    private EditText etuser, etpass;
    private Button btnlogin, btnregister;
    private Switch sw;
    private String username = "", password = "", sessionID = "";
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        sp=getSharedPreferences("user",MODE_PRIVATE);
        //读取信息
        readSP();
    }

    private void readSP() {
        SharedPreferences.Editor editor=sp.edit();
        boolean remember=sp.getBoolean("remember",false);
        sw.setChecked(remember);
        if (remember){
            etuser.setText(sp.getString("username",""));
            etpass.setText(sp.getString("password",""));
        }

    }

    private void initViews() {
        etuser = findViewById(R.id.etname);
        etpass = findViewById(R.id.etpass);
        btnlogin = findViewById(R.id.btnlogin);
        btnregister = findViewById(R.id.btnreg);
        sw = findViewById(R.id.switch1);
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //登录的代码

                username = etuser.getText().toString();
                password = etpass.getText().toString();
                LoginModel loginModel = new LoginModel();
                loginModel.getLoginResult(username, password, loginListener);
            }
        });
        btnregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //注册代码
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });

    }


     LoginListener loginListener = new LoginListener() {


        @Override
        public void onResponse(UserBean loginBean) {
            sessionID = loginBean.getSessionid();
            System.out.println("----LoginActivity sessionID=" + sessionID);
            if (sessionID != null) {
                    save();
                Toast.makeText(LoginActivity.this, "登录成功" + sessionID, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };


    //存储信息
    private void save() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.putString("sessionID",sessionID);
        editor.commit();
    }
}